package userShareModuleApi

import (
	"KumquatDrivesServices/common/res"
	"KumquatDrivesServices/global"
	"KumquatDrivesServices/model"
	"KumquatDrivesServices/utils"
	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
)

type cancelShareFileRequest struct {
	ShareId []string `json:"shareId" binding:"required"`
}

func (UserShareModuleApi) CancelShareFile(c *gin.Context) {
	var req cancelShareFileRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		logrus.Errorf("CancelShareFile ShouldBindJSON err:%v", err)
		res.FailWithCode(res.PARAM_ERR, c)
		return
	}
	userId := utils.GetUserId(c)
	tx := global.DB.Begin()
	query := tx.Model(&model.ShareModel{}).Where("share_id in (?) and  user_id=?", req.ShareId, userId)
	if err := query.Delete(&model.ShareModel{}).Error; err != nil {
		tx.Rollback()
		logrus.Errorf("CancelShareFile Delete err:%v", err)
		res.FailWithCode(res.SYS_ERR, c)
	}
	if err := tx.Commit().Error; err != nil {
		logrus.Errorf("CancelShareFile Commit err:%v", err)
		res.FailWithCode(res.SYS_ERR, c)
		return
	}
	res.OkWithData(res.EmptyData, c)
}
